package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class SetmealServiceImpl implements SetmealService {


    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;


    /**
     * 根据分类ID查询套餐
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }


    /**
     * 根据套餐ID查询对应的菜品列表
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemBySetmealId(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }


    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        // 对象属性拷贝
        BeanUtils.copyProperties(setmealDTO,setmeal);

        // 向套餐表中添加数据,并返回套餐ID
        setmealMapper.insertSetmeal(setmeal);

        // 获取返回的套餐ID
        Long setmealId = setmeal.getId();

        // 获取套餐关联的菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        // 判断套餐中是否有数据
        if(setmealDishes.size() != 0 || setmealDishes != null){
            // 遍历数据设置套餐id
            setmealDishes.forEach(setmealDish ->{
                setmealDish.setId(setmealId);
            });
            // 向套餐和菜品关系表中添加数据
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }


    /**
     * 根据ID查询套餐
     * @param id
     * @return
     */
    @Transactional
    @Override
    public SetmealVO selectSetmealById(Long id) {
        // 查询套餐信息
        Setmeal setmeal = setmealMapper.selectSetmeal(id);
        if(setmeal == null){
            return null;
        }

        // 查询套餐和菜品的关系信息
        List<SetmealDish> listSetmealDish = setmealDishMapper.selectStemealAndDishBySetmealId(id);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);

        // 将套餐和菜品的信息进行封装
        if(listSetmealDish != null && listSetmealDish.size() > 0){
            setmealVO.setSetmealDishes(listSetmealDish);
        }
        return setmealVO;
    }


    /**
     * 设置套餐状态
     * @param status
     * @param id
     */
    @Override
    public void setStartOrStop(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.updateSetmeal(setmeal);
    }


    /**
     * 修改套餐信息和关联的菜品信息
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        // 修改套餐信息
        setmealMapper.updateSetmeal(setmeal);

        // 修改套餐和菜品的关联信息
        setmealDishMapper.deleteSetmealDish(setmeal.getId());   // 通过套餐ID删除原有的关联信息
        // 重新添加关联数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && setmealDishes.size() > 0){
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }


    /**
     * 批量删除套餐信息
     * @param ids
     */
    @Transactional
    @Override
    public void deleteStemealByIds(List<Long> ids) {
        // 判断当前套餐状态是否可以删除
        for (Long id : ids) {
            // 通过id查询数据
            Setmeal setmeal = setmealMapper.selectSetmeal(id);
            if(setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        // 删除菜品信息
        setmealMapper.deleteStemealByIds(ids);
        // 删除菜品和套餐的关联信息
        setmealDishMapper.deleteSetmealDishByIds(ids);
    }


    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult setmealPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        // 设置默认页码色显示的条数
        if(setmealPageQueryDTO.getPage() == 0){
            setmealPageQueryDTO.setPage(1);
        }
        if(setmealPageQueryDTO.getPageSize() == 0){
            setmealPageQueryDTO.setPageSize(10);
        }
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.selectSetmealPage(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }
}
